home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / lisp / mh-e / mh-utils.el < prev    next >
Encoding:
Text File  |  1995-06-06  |  32.6 KB  |  911 lines

  1. ;;; mh-utils.el --- mh-e code needed for both sending and reading
  2. ;; Time-stamp: <95/05/29 16:41:44 gildea>
  3.  
  4. ;; Copyright (C) 1993, 1995 Free Software Foundation, Inc.
  5.  
  6. ;; This file is part of GNU Emacs.
  7.  
  8. ;; GNU Emacs is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 2, or (at your option)
  11. ;; any later version.
  12.  
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ;; GNU General Public License for more details.
  17.  
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  20. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. ;;; Commentary:
  23.  
  24. ;; Internal support for mh-e package.
  25.  
  26. ;;; Change Log:
  27.  
  28. ;; $Id: mh-utils.el,v 1.9 95/05/29 20:42:19 gildea Exp $
  29.  
  30. ;;; Code:
  31.  
  32. ;;; Set for local environment:
  33. ;;; mh-progs and mh-lib used to be set in paths.el, which tried to
  34. ;;; figure out at build time which of several possible directories MH
  35. ;;; was installed into.  But if you installed MH after building Emacs,
  36. ;;; this would almost certainly be wrong, so now we do it at run time.
  37.  
  38. (defvar mh-progs nil
  39.   "Directory containing MH commands, such as inc, repl, and rmm.")
  40.  
  41. (defvar mh-lib nil
  42.   "Directory containing the MH library.
  43. This directory contains, among other things,
  44. the mhl program and the components file.")
  45.  
  46. ;;;###autoload
  47. (put 'mh-progs 'risky-local-variable t)
  48. ;;;###autoload
  49. (put 'mh-lib 'risky-local-variable t)
  50.  
  51. ;;; User preferences:
  52.  
  53. (defvar mh-auto-folder-collect t
  54.   "*Whether to start collecting MH folder names immediately in the background.
  55. Non-nil means start a background process collecting the names of all
  56. folders as soon as mh-e is loaded.")
  57.  
  58. (defvar mh-recursive-folders nil
  59.   "*If non-nil, then commands which operate on folders do so recursively.")
  60.  
  61. (defvar mh-clean-message-header nil
  62.   "*Non-nil means clean headers of messages that are displayed or inserted.
  63. The variables `mh-visible-headers' and `mh-invisible-headers' control what
  64. is removed.")
  65.  
  66. (defvar mh-visible-headers nil
  67.   "*If non-nil, contains a regexp specifying the headers to keep when cleaning.
  68. Only used if `mh-clean-message-header' is non-nil.  Setting this variable
  69. overrides `mh-invisible-headers'.")
  70.  
  71. (defvar mh-invisible-headers
  72.   "^Received: \\|^Message-Id: \\|^Remailed-\\|^Via: \\|^Mail-from: \\|^Return-Path: \\|^Delivery-Date: \\|^In-Reply-To: \\|^Resent-"
  73.   "Regexp matching lines in a message header that are not to be shown.
  74. If `mh-visible-headers' is non-nil, it is used instead to specify what
  75. to keep.")
  76.  
  77. (defvar mh-bury-show-buffer t
  78.   "*Non-nil means that the displayed show buffer for a folder is buried.")
  79.  
  80. (defvar mh-summary-height 4
  81.   "*Number of lines in MH-Folder window (including the mode line).")
  82.  
  83. (defvar mh-msg-number-regexp "^ *\\([0-9]+\\)"
  84.   "Regexp to find the number of a message in a scan line.
  85. The message's number must be surrounded with \\( \\)")
  86.  
  87. (defvar mh-msg-search-regexp "^[^0-9]*%d[^0-9]"
  88.   "Format string containing a regexp matching the scan listing for a message.
  89. The desired message's number will be an argument to format.")
  90.  
  91. (defvar mhl-formfile nil
  92.   "*Name of format file to be used by mhl to show and print messages.
  93. A value of T means use the default format file.
  94. Nil means don't use mhl to format messages when showing; mhl is still used,
  95. with the default format file, to format messages when printing them.
  96. The format used should specify a non-zero value for overflowoffset so
  97. the message continues to conform to RFC 822 and mh-e can parse the headers.")
  98.  
  99. (defvar mh-default-folder-for-message-function nil
  100.   "Function to select a default folder for refiling or Fcc.
  101. If set to a function, that function is called with no arguments by
  102. `\\[mh-refile-msg]' and `\\[mh-to-fcc]' to get a default when
  103. prompting the user for a folder.  The function is called from within a
  104. save-excursion, with point at the start of the message.  It should
  105. return the folder to offer as the refile or Fcc folder, as a string
  106. with a leading `+' sign.  It can also return an empty string to use no
  107. default, or NIL to calculate the default the usual way.
  108. NOTE: This variable is not an ordinary hook;
  109. It may not be a list of functions.")
  110.  
  111. (defvar mh-find-path-hook nil
  112.   "Invoked by mh-find-path while reading the user's MH profile.")
  113.  
  114. (defvar mh-folder-list-change-hook nil
  115.   "Invoked whenever the cached folder list `mh-folder-list' is changed.")
  116.  
  117. (defvar mh-show-buffer-mode-line-buffer-id "{show-%s} %d"
  118.   "Format string to produce `mode-line-buffer-identification' for show buffers.
  119. First argument is folder name.  Second is message number.")
  120.  
  121. (defvar mh-cmd-note 4
  122.   "Offset to insert notation.")
  123.  
  124. (defvar mh-note-seq "%"
  125.   "String whose first character is used to notate messages in a sequence.")
  126.  
  127. ;;; Internal bookkeeping variables:
  128.  
  129. ;; The value of `mh-folder-list-change-hook' is called whenever
  130. ;; mh-folder-list variable is set.
  131. (defvar mh-folder-list nil)        ;List of folder names for completion.
  132.  
  133. ;; Cached value of the `Path:' component in the user's MH profile.
  134. (defvar mh-user-path nil)        ;User's mail folder directory.
  135.  
  136. ;; An mh-draft-folder of NIL means do not use a draft folder.
  137. ;; Cached value of the `Draft-Folder:' component in the user's MH profile.
  138. (defvar mh-draft-folder nil)        ;Name of folder containing draft messages.
  139.  
  140. ;; Cached value of the `Unseen-Sequence:' component in the user's MH profile.
  141. (defvar mh-unseen-seq nil)        ;Name of the Unseen sequence.
  142.  
  143. ;; Cached value of the `Previous-Sequence:' component in the user's MH profile.
  144. (defvar mh-previous-seq nil)        ;Name of the Previous sequence.
  145.  
  146. ;; Cached value of the `Inbox:' component in the user's MH profile,
  147. ;; or "+inbox" if no such component.
  148. (defvar mh-inbox nil)            ;Name of the Inbox folder.
  149.  
  150. (defconst mh-temp-buffer " *mh-temp*")    ;Name of mh-e scratch buffer.
  151.  
  152. (defvar mh-previous-window-config nil)    ;Window configuration before mh-e command.
  153.  
  154. ;;; Internal variables local to a folder.
  155.  
  156. (defvar mh-current-folder nil)        ;Name of current folder, a string.
  157.  
  158. (defvar mh-show-buffer nil)        ;Buffer that displays message for this folder.
  159.  
  160. (defvar mh-folder-filename nil)        ;Full path of directory for this folder.
  161.  
  162. (defvar mh-showing nil)            ;If non-nil, show the message in a separate window.
  163.  
  164. ;;; This holds a documentation string used by describe-mode.
  165. (defun mh-showing ()
  166.   "When moving to a new message in the Folder window,
  167. also show it in a separate Show window."
  168.   nil)
  169.  
  170. (defvar mh-seq-list nil)        ;The sequences of this folder.  An alist of (seq . msgs).
  171.  
  172. (defvar mh-seen-list nil)        ;List of displayed messages to be removed from the Unseen sequence.
  173.  
  174. ;; If non-nil, show buffer contains message with all headers.
  175. ;; If nil, show buffer contains message processed normally.
  176. (defvar mh-showing-with-headers nil)    ;Showing message with headers or normally.
  177.  
  178.  
  179. ;;; mh-e macros
  180.  
  181. (defmacro with-mh-folder-updating (save-modification-flag-p &rest body)
  182.   ;; Format is (with-mh-folder-updating (SAVE-MODIFICATION-FLAG-P) &body BODY).
  183.   ;; Execute BODY, which can modify the folder buffer without having to
  184.   ;; worry about file locking or the read-only flag, and return its result.
  185.   ;; If SAVE-MODIFICATION-FLAG-P is non-nil, the buffer's modification
  186.   ;; flag is unchanged, otherwise it is cleared.
  187.   (setq save-modification-flag-p (car save-modification-flag-p)) ; CL style
  188.   (` (prog1
  189.      (let ((mh-folder-updating-mod-flag (buffer-modified-p))
  190.            (buffer-read-only nil)
  191.            (buffer-file-name nil))    ;don't let the buffer get locked
  192.        (prog1
  193.            (progn
  194.          (,@ body))
  195.          (mh-set-folder-modified-p mh-folder-updating-mod-flag)))
  196.        (,@ (if (not save-modification-flag-p)
  197.            '((mh-set-folder-modified-p nil)))))))
  198.  
  199. (put 'with-mh-folder-updating 'lisp-indent-hook 1)
  200.  
  201. (defmacro mh-in-show-buffer (show-buffer &rest body)
  202.   ;; Format is (mh-in-show-buffer (SHOW-BUFFER) &body BODY).
  203.   ;; Display buffer SHOW-BUFFER in other window and execute BODY in it.
  204.   ;; Stronger than save-excursion, weaker than save-window-excursion.
  205.   (setq show-buffer (car show-buffer))    ; CL style
  206.   (` (let ((mh-in-show-buffer-saved-window (selected-window)))
  207.        (switch-to-buffer-other-window (, show-buffer))
  208.        (if mh-bury-show-buffer (bury-buffer (current-buffer)))
  209.        (unwind-protect
  210.        (progn
  211.          (,@ body))
  212.      (select-window mh-in-show-buffer-saved-window)))))
  213.  
  214. (put 'mh-in-show-buffer 'lisp-indent-hook 1)
  215.  
  216. (defmacro mh-make-seq (name msgs) (list 'cons name msgs))
  217.  
  218. (defmacro mh-seq-name (pair) (list 'car pair))
  219.  
  220. (defmacro mh-seq-msgs (pair) (list 'cdr pair))
  221.  
  222.  
  223. ;;; Ensure new buffers won't get this mode if default-major-mode is nil.
  224. (put 'mh-show-mode 'mode-class 'special)
  225.  
  226. (defun mh-show-mode ()
  227.   "Major mode for showing messages in mh-e.
  228. The value of mh-show-mode-hook is called when a new message is displayed."
  229.   (kill-all-local-variables)
  230.   (setq major-mode 'mh-show-mode)
  231.   (mh-set-mode-name "MH-Show")
  232.   (run-hooks 'mh-show-mode-hook))
  233.  
  234.  
  235. (defun mh-maybe-show (&optional msg)
  236.   ;; If in showing mode, then display the message pointed to by the cursor.
  237.   (if mh-showing (mh-show msg)))
  238.  
  239. (defun mh-show (&optional message)
  240.   "Show MESSAGE (default: message at cursor).
  241. Force a two-window display with the folder window on top (size
  242. mh-summary-height) and the show buffer below it.
  243. If the message is already visible, display the start of the message.
  244.  
  245. Display of the message is controlled by setting the variables
  246. `mh-clean-message-header' and `mhl-formfile'.  The default behavior is
  247. to scroll uninteresting headers off the top of the window.
  248. Type \"\\[mh-header-display]\" to see the message with all its headers."
  249.   (interactive)
  250.   (and mh-showing-with-headers
  251.        (or mhl-formfile mh-clean-message-header)
  252.        (mh-invalidate-show-buffer))
  253.   (mh-show-msg message))
  254.  
  255.  
  256. (defun mh-show-msg (msg)
  257.   (if (not msg)
  258.       (setq msg (mh-get-msg-num t)))
  259.   (setq mh-showing t)
  260.   (let ((folder mh-current-folder)
  261.     (clean-message-header mh-clean-message-header)
  262.     (show-window (get-buffer-window mh-show-buffer)))
  263.     (if (not (eql (next-window (minibuffer-window)) (selected-window)))
  264.     (delete-other-windows))        ; force ourself to the top window
  265.     (mh-in-show-buffer (mh-show-buffer)
  266.       (if (and show-window
  267.            (equal (mh-msg-filename msg folder) buffer-file-name))
  268.       (progn            ;just back up to start
  269.         (goto-char (point-min))
  270.         (if (not clean-message-header)
  271.         (mh-start-of-uncleaned-message)))
  272.     (mh-display-msg msg folder))))
  273.   (if (not (= (1+ (window-height)) (screen-height))) ;not horizontally split
  274.       (shrink-window (- (window-height) mh-summary-height)))
  275.   (mh-recenter nil)
  276.   (if (not (memq msg mh-seen-list)) (setq mh-seen-list (cons msg mh-seen-list)))
  277.   (run-hooks 'mh-show-hook))
  278.  
  279.  
  280. (defun mh-display-msg (msg-num folder)
  281.   ;; Display message NUMBER of FOLDER.
  282.   ;; Sets the current buffer to the show buffer.
  283.   (set-buffer folder)
  284.   ;; Bind variables in folder buffer in case they are local
  285.   (let ((formfile mhl-formfile)
  286.     (clean-message-header mh-clean-message-header)
  287.     (invisible-headers mh-invisible-headers)
  288.     (visible-headers mh-visible-headers)
  289.     (msg-filename (mh-msg-filename msg-num))
  290.     (show-buffer mh-show-buffer))
  291.     (if (not (file-exists-p msg-filename))
  292.     (error "Message %d does not exist" msg-num))
  293.     (set-buffer show-buffer)
  294.     (cond ((not (equal msg-filename buffer-file-name))
  295.        (mh-unvisit-file)
  296.        (erase-buffer)
  297.        ;; Changing contents, so this hook needs to be reinitialized.
  298.        ;; pgp.el uses this.
  299.        (if (boundp 'write-contents-hooks) ;Emacs 19
  300.            (setq write-contents-hooks nil))
  301.        (if formfile
  302.            (mh-exec-lib-cmd-output "mhl" "-nobell" "-noclear"
  303.                        (if (stringp formfile)
  304.                        (list "-form" formfile))
  305.                        msg-filename)
  306.          (insert-file-contents msg-filename))
  307.        (goto-char (point-min))
  308.        (cond (clean-message-header
  309.           (mh-clean-msg-header (point-min)
  310.                        invisible-headers
  311.                        visible-headers)
  312.           (goto-char (point-min)))
  313.          (t
  314.           (mh-start-of-uncleaned-message)))
  315.        ;; the parts of visiting we want to do (no locking)
  316.        (or (eq buffer-undo-list t)    ;don't save undo info for prev msgs
  317.            (setq buffer-undo-list nil))
  318.        (set-buffer-modified-p nil)
  319.        (set-buffer-auto-saved)
  320.        ;; the parts of set-visited-file-name we want to do (no locking)
  321.        (setq buffer-file-name msg-filename)
  322.        (setq buffer-backed-up nil)
  323.        (auto-save-mode 1)
  324.        (set-mark nil)
  325.        (mh-show-mode)
  326.        (setq mode-line-buffer-identification
  327.          (list (format mh-show-buffer-mode-line-buffer-id
  328.                    folder msg-num)))
  329.        (set-buffer folder)
  330.        (setq mh-showing-with-headers nil)))))
  331.  
  332. (defun mh-start-of-uncleaned-message ()
  333.   ;; position uninteresting headers off the top of the window
  334.   (let ((case-fold-search t))
  335.     (re-search-forward
  336.      "^To:\\|^Cc:\\|^From:\\|^Subject:\\|^Date:" nil t)
  337.     (beginning-of-line)
  338.     (mh-recenter 0)))
  339.  
  340.  
  341. (defun mh-invalidate-show-buffer ()
  342.   ;; Invalidate the show buffer so we must update it to use it.
  343.   (if (get-buffer mh-show-buffer)
  344.       (save-excursion
  345.     (set-buffer mh-show-buffer)
  346.     (mh-unvisit-file))))
  347.  
  348.  
  349. (defun mh-unvisit-file ()
  350.   ;; Separate current buffer from the message file it was visiting.
  351.   (or (not (buffer-modified-p))
  352.       (null buffer-file-name)        ;we've been here before
  353.       (yes-or-no-p (format "Message %s modified; flush changes? "
  354.                (file-name-nondirectory buffer-file-name)))
  355.       (error "Flushing changes not confirmed"))
  356.   (clear-visited-file-modtime)
  357.   (unlock-buffer)
  358.   (setq buffer-file-name nil))
  359.  
  360.   
  361. (defun mh-get-msg-num (error-if-no-message)
  362.   ;; Return the message number of the displayed message.  If the argument
  363.   ;; ERROR-IF-NO-MESSAGE is non-nil, then complain if the cursor is not
  364.   ;; pointing to a message.
  365.   (save-excursion
  366.     (beginning-of-line)
  367.     (cond ((looking-at mh-msg-number-regexp)
  368.        (string-to-int (buffer-substring (match-beginning 1)
  369.                         (match-end 1))))
  370.       (error-if-no-message
  371.        (error "Cursor not pointing to message"))
  372.       (t nil))))
  373.  
  374.  
  375. (defun mh-msg-filename (msg &optional folder)
  376.   ;; Return the file name of MESSAGE in FOLDER (default current folder).
  377.   (expand-file-name (int-to-string msg)
  378.             (if folder
  379.             (mh-expand-file-name folder)
  380.             mh-folder-filename)))
  381.  
  382.  
  383. (defun mh-clean-msg-header (start invisible-headers visible-headers)
  384.   ;; Flush extraneous lines in a message header, from the given POINT to the
  385.   ;; end of the message header.  If VISIBLE-HEADERS is non-nil, it contains a
  386.   ;; regular expression specifying the lines to display, otherwise
  387.   ;; INVISIBLE-HEADERS contains a regular expression specifying lines to
  388.   ;; delete from the header.
  389.   (let ((case-fold-search t))
  390.     (save-restriction
  391.       (goto-char start)
  392.       (if (search-forward "\n\n" nil 'move)
  393.       (backward-char 1))
  394.       (narrow-to-region start (point))
  395.       (goto-char (point-min))
  396.       (if visible-headers
  397.       (while (< (point) (point-max))
  398.         (cond ((looking-at visible-headers)
  399.            (forward-line 1)
  400.            (while (looking-at "[ \t]") (forward-line 1)))
  401.           (t
  402.             (mh-delete-line 1)
  403.             (while (looking-at "[ \t]")
  404.               (mh-delete-line 1)))))
  405.       (while (re-search-forward invisible-headers nil t)
  406.         (beginning-of-line)
  407.         (mh-delete-line 1)
  408.         (while (looking-at "[ \t]")
  409.           (mh-delete-line 1))))
  410.       (unlock-buffer))))
  411.  
  412.  
  413. (defun mh-recenter (arg)
  414.   ;; Like recenter but with two improvements: nil arg means recenter,
  415.   ;; and only does anything if the current buffer is in the selected
  416.   ;; window.  (Commands like save-some-buffers can make this false.)
  417.   (if (eql (get-buffer-window (current-buffer))
  418.        (selected-window))
  419.       (recenter (if arg arg '(t)))))
  420.  
  421.  
  422. (defun mh-delete-line (lines)
  423.   ;; Delete version of kill-line.
  424.   (delete-region (point) (save-excursion (forward-line lines) (point))))
  425.  
  426.  
  427. (defun mh-notate (msg notation offset)
  428.   ;; Marks MESSAGE with the character NOTATION at position OFFSET.
  429.   ;; Null MESSAGE means the message that the cursor points to.
  430.   (save-excursion
  431.     (if (or (null msg)
  432.         (mh-goto-msg msg t t))
  433.     (with-mh-folder-updating (t)
  434.       (beginning-of-line)
  435.       (forward-char offset)
  436.       (delete-char 1)
  437.       (insert notation)))))
  438.  
  439.  
  440. (defun mh-goto-msg (number &optional no-error-if-no-message dont-show)
  441.   "Position the cursor at message NUMBER.
  442. Optional non-nil second argument means return nil instead of
  443. signaling an error if message does not exist.
  444. Non-nil third argument means not to show the message."
  445.   (interactive "NGo to message: ")
  446.   (setq number (prefix-numeric-value number)) ;Emacs 19
  447.   (let ((cur-msg (mh-get-msg-num nil))
  448.     (starting-place (point))
  449.     (msg-pattern (mh-msg-search-pat number)))
  450.     (cond ((cond ((and cur-msg (= cur-msg number)) t)
  451.          ((and cur-msg
  452.                (< cur-msg number)
  453.                (re-search-forward msg-pattern nil t)) t)
  454.          ((and cur-msg
  455.                (> cur-msg number)
  456.                (re-search-backward msg-pattern nil t)) t)
  457.          (t            ; Do thorough search of buffer
  458.           (goto-char (point-max))
  459.           (re-search-backward msg-pattern nil t)))
  460.         (beginning-of-line)
  461.         (if (not dont-show) (mh-maybe-show number))
  462.         t)
  463.       (t
  464.        (goto-char starting-place)
  465.        (if (not no-error-if-no-message)
  466.            (error "No message %d" number))
  467.        nil))))
  468.  
  469. (defun mh-msg-search-pat (n)
  470.   ;; Return a search pattern for message N in the scan listing.
  471.   (format mh-msg-search-regexp n))
  472.  
  473.  
  474. (defun mh-get-profile-field (field)
  475.   ;; Find and return the value of FIELD in the current buffer.
  476.   ;; Returns NIL if the field is not in the buffer.
  477.   (let ((case-fold-search t))
  478.     (goto-char (point-min))
  479.     (cond ((not (re-search-forward (format "^%s" field) nil t)) nil)
  480.       ((looking-at "[\t ]*$") nil)
  481.       (t
  482.        (re-search-forward "[\t ]*\\([^\t \n].*\\)$" nil t)
  483.        (let ((start (match-beginning 1)))
  484.          (end-of-line)
  485.          (buffer-substring start (point)))))))
  486.  
  487.  
  488. (defun mh-find-path ()
  489.   ;; Set mh-progs and mh-lib.
  490.   ;; (This step is necessary if MH was installed after this Emacs was dumped.)
  491.   ;; From profile file, set mh-user-path, mh-draft-folder,
  492.   ;; mh-unseen-seq, mh-previous-seq, mh-inbox.
  493.   (mh-find-progs)
  494.   (save-excursion
  495.     ;; Be sure profile is fully expanded before switching buffers
  496.     (let ((profile (expand-file-name (or (getenv "MH") "~/.mh_profile"))))
  497.       (set-buffer (get-buffer-create mh-temp-buffer))
  498.       (setq buffer-offer-save nil)    ;for people who set default to t
  499.       (erase-buffer)
  500.       (condition-case err
  501.       (insert-file-contents profile)
  502.     (file-error
  503.      (mh-install profile err)))
  504.       (setq mh-user-path (mh-get-profile-field "Path:"))
  505.       (if (not mh-user-path)
  506.       (setq mh-user-path "Mail"))
  507.       (setq mh-user-path
  508.         (file-name-as-directory
  509.          (expand-file-name mh-user-path (expand-file-name "~"))))
  510.       (setq mh-draft-folder (mh-get-profile-field "Draft-Folder:"))
  511.       (if mh-draft-folder
  512.       (progn
  513.         (if (not (mh-folder-name-p mh-draft-folder))
  514.         (setq mh-draft-folder (format "+%s" mh-draft-folder)))
  515.         (if (not (file-exists-p (mh-expand-file-name mh-draft-folder)))
  516.         (error "Draft folder \"%s\" not found.  Create it and try again."
  517.                (mh-expand-file-name mh-draft-folder)))))
  518.       (setq mh-inbox (mh-get-profile-field "Inbox:"))
  519.       (cond ((not mh-inbox)
  520.          (setq mh-inbox "+inbox"))
  521.         ((not (mh-folder-name-p mh-inbox))
  522.          (setq mh-inbox (format "+%s" mh-inbox))))
  523.       (setq mh-unseen-seq (mh-get-profile-field "Unseen-Sequence:"))
  524.       (if mh-unseen-seq
  525.       (setq mh-unseen-seq (intern mh-unseen-seq))
  526.     (setq mh-unseen-seq 'unseen))    ;old MH default?
  527.       (setq mh-previous-seq (mh-get-profile-field "Previous-Sequence:"))
  528.       (if mh-previous-seq
  529.       (setq mh-previous-seq (intern mh-previous-seq)))
  530.       (run-hooks 'mh-find-path-hook))))
  531.  
  532. (defun mh-find-progs ()
  533.   (or (file-exists-p (expand-file-name "inc" mh-progs))
  534.       (setq mh-progs
  535.         (or (mh-path-search exec-path "inc")
  536.         (mh-path-search '("/usr/local/bin/mh/"
  537.                   "/usr/local/mh/"
  538.                   "/usr/bin/mh/" ;Ultrix 4.2
  539.                   "/usr/new/mh/" ;Ultrix <4.2
  540.                   "/usr/contrib/mh/bin/" ;BSDI
  541.                   "/usr/local/bin/"
  542.                   )
  543.                 "inc")
  544.         mh-progs
  545.         "/usr/local/bin/")))
  546.   (or (file-exists-p (expand-file-name "mhl" mh-lib))
  547.       (setq mh-lib
  548.         (or (mh-path-search '("/usr/local/lib/mh/"
  549.                   "/usr/local/mh/lib/"
  550.                   "/usr/local/bin/mh/"
  551.                   "/usr/lib/mh/" ;Ultrix 4.2
  552.                   "/usr/new/lib/mh/" ;Ultrix <4.2
  553.                   "/usr/contrib/mh/lib/" ;BSDI
  554.                   )
  555.                 "mhl")
  556.         (mh-path-search exec-path "mhl") ;unlikely
  557.         mh-lib
  558.         "/usr/local/lib/mh/"))))
  559.  
  560. (defun mh-path-search (path file)
  561.   ;; Search PATH, a list of directory names, for FILE.
  562.   ;; Returns the element of PATH that contains FILE, or nil if not found.
  563.   (while (and path
  564.           (not (file-exists-p (expand-file-name file (car path)))))
  565.     (setq path (cdr path)))
  566.   (car path))
  567.  
  568. (defun mh-install (profile error-val)
  569.   ;; Called to do error recovery if we fail to read the profile file.
  570.   ;; If possible, initialize the MH environment.
  571.   (if (or (getenv "MH")
  572.       (file-exists-p profile))
  573.       (error "Cannot read MH profile \"%s\": %s"
  574.          profile (car (cdr (cdr error-val)))))
  575.   ;; The "install-mh" command will output a short note which
  576.   ;; mh-exec-cmd will display to the user.
  577.   ;; The MH 5 version of install-mh might try prompt the user
  578.   ;; for information, which would fail here.
  579.   (mh-exec-cmd (expand-file-name "install-mh" mh-lib) "-auto")
  580.   ;; now try again to read the profile file
  581.   (erase-buffer)
  582.   (condition-case err
  583.       (insert-file-contents profile)
  584.     (file-error
  585.      (error "Cannot read MH profile \"%s\": %s"
  586.         profile (car (cdr (cdr err)))))))
  587.  
  588.  
  589. (defun mh-set-folder-modified-p (flag)
  590.   ;; Mark current folder as modified or unmodified according to FLAG.
  591.   (set-buffer-modified-p flag))
  592.  
  593.  
  594. (defun mh-find-seq (name) (assoc name mh-seq-list))
  595.  
  596. (defun mh-seq-to-msgs (seq)
  597.   ;; Return a list of the messages in SEQUENCE.
  598.   (mh-seq-msgs (mh-find-seq seq)))
  599.  
  600.  
  601. (defun mh-add-msgs-to-seq (msgs seq &optional internal-flag)
  602.   ;; Add MESSAGE(s) to the SEQUENCE.  If optional FLAG is non-nil, do not mark
  603.   ;; the message in the scan listing or inform MH of the addition.
  604.   (let ((entry (mh-find-seq seq)))
  605.     (if (and msgs (atom msgs)) (setq msgs (list msgs)))
  606.     (if (null entry)
  607.     (setq mh-seq-list (cons (mh-make-seq seq msgs) mh-seq-list))
  608.     (if msgs (setcdr entry (append msgs (mh-seq-msgs entry)))))
  609.     (cond ((not internal-flag)
  610.        (mh-add-to-sequence seq msgs)
  611.        (mh-notate-seq seq mh-note-seq (1+ mh-cmd-note))))))
  612.  
  613. (autoload 'mh-add-to-sequence "mh-seq")
  614. (autoload 'mh-notate-seq "mh-seq")
  615. (autoload 'mh-read-seq-default "mh-seq")
  616. (autoload 'mh-map-to-seq-msgs "mh-seq")
  617.  
  618.  
  619. (defun mh-set-mode-name (mode-name-string)
  620.   ;; Set the mode-name and ensure that the mode line is updated.
  621.   (setq mode-name mode-name-string)
  622.   ;; Force redisplay of all buffers' mode lines to be considered.
  623.   (save-excursion (set-buffer (other-buffer)))
  624.   (set-buffer-modified-p (buffer-modified-p)))
  625.  
  626.  
  627. (defun mh-prompt-for-folder (prompt default can-create)
  628.   ;; Prompt for a folder name with PROMPT.  Returns the folder's name as a
  629.   ;; string.  DEFAULT is used if the folder exists and the user types return.
  630.   ;; If the CAN-CREATE flag is t, then a non-existent folder is made.
  631.   (if (null default)
  632.       (setq default ""))
  633.   (let* ((prompt (format "%s folder%s" prompt
  634.              (if (equal "" default)
  635.                  "? "
  636.                  (format " [%s]? " default))))
  637.      read-name folder-name)
  638.     (if (null mh-folder-list)
  639.     (mh-set-folder-list))
  640.     (while (and (setq read-name (completing-read prompt mh-folder-list
  641.                         nil nil "+"))
  642.         (equal read-name "")
  643.         (equal default "")))
  644.     (cond ((or (equal read-name "") (equal read-name "+"))
  645.        (setq read-name default))
  646.       ((not (mh-folder-name-p read-name))
  647.        (setq read-name (format "+%s" read-name))))
  648.     (setq folder-name read-name)
  649.     (cond ((and (> (length folder-name) 0)
  650.         (eql (aref folder-name (1- (length folder-name))) ?/))
  651.        (setq folder-name (substring folder-name 0 -1))))
  652.     (let ((new-file-p (not (file-exists-p (mh-expand-file-name folder-name)))))
  653.       (cond ((and new-file-p
  654.           (y-or-n-p
  655.            (format "Folder %s does not exist.  Create it? " folder-name)))
  656.          (message "Creating %s" folder-name)
  657.          (call-process "mkdir" nil nil nil (mh-expand-file-name folder-name))
  658.          (message "Creating %s...done" folder-name)
  659.          (setq mh-folder-list (cons (list read-name) mh-folder-list))
  660.          (run-hooks 'mh-folder-list-change-hook))
  661.         (new-file-p
  662.          (error "Folder %s is not created" folder-name))
  663.         ((and (null (assoc read-name mh-folder-list))
  664.           (null (assoc (concat read-name "/") mh-folder-list)))
  665.          (setq mh-folder-list (cons (list read-name) mh-folder-list))
  666.          (run-hooks 'mh-folder-list-change-hook))))
  667.     folder-name))
  668.  
  669.  
  670. (defvar mh-make-folder-list-process nil) ;The background process collecting the folder list.
  671.  
  672. (defvar mh-folder-list-temp nil)    ;mh-folder-list as it is being built.
  673.  
  674. (defvar mh-folder-list-partial-line "")    ;Start of last incomplete line from folder process.
  675.  
  676. (defun mh-set-folder-list ()
  677.   ;; Sets mh-folder-list correctly.
  678.   ;; A useful function for the command line or for when you need to
  679.   ;; sync by hand.  Format is in a form suitable for completing read.
  680.   (message "Collecting folder names...")
  681.   (if (not mh-make-folder-list-process)
  682.       (mh-make-folder-list-background))
  683.   (while (eq (process-status mh-make-folder-list-process) 'run)
  684.     (accept-process-output mh-make-folder-list-process))
  685.   (setq mh-folder-list mh-folder-list-temp)
  686.   (run-hooks 'mh-folder-list-change-hook)
  687.   (setq mh-folder-list-temp nil)
  688.   (delete-process mh-make-folder-list-process)
  689.   (setq mh-make-folder-list-process nil)
  690.   (message "Collecting folder names...done"))
  691.  
  692. (defun mh-make-folder-list-background ()
  693.   ;; Start a background process to compute a list of the user's folders.
  694.   ;; Call mh-set-folder-list to wait for the result.
  695.   (cond
  696.    ((not mh-make-folder-list-process)
  697.     (mh-find-progs)
  698.     (let ((process-connection-type nil))
  699.       (setq mh-make-folder-list-process
  700.         (start-process "folders" nil (expand-file-name "folders" mh-progs)
  701.                "-fast"
  702.                (if mh-recursive-folders
  703.                    "-recurse"
  704.                  "-norecurse")))
  705.       (set-process-filter mh-make-folder-list-process
  706.               'mh-make-folder-list-filter)
  707.       (process-kill-without-query mh-make-folder-list-process)))))
  708.  
  709. (defun mh-make-folder-list-filter (process output)
  710.   ;; parse output from "folders -fast"
  711.   (let ((position 0)
  712.     (line-end t)
  713.     new-folder)
  714.     (while line-end
  715.       (setq line-end (string-match "\n" output position))
  716.       (cond
  717.        (line-end            ;make sure got complete line
  718.     (setq new-folder (format "+%s%s"
  719.                  mh-folder-list-partial-line
  720.                  (substring output position line-end)))
  721.     (setq mh-folder-list-partial-line "")
  722.     ;; is new folder a subfolder of previous?
  723.     (if (and mh-folder-list-temp
  724.          (string-match (regexp-quote
  725.                 (concat (car (car mh-folder-list-temp)) "/"))
  726.                    new-folder))
  727.         ;; append slash to parent folder for better completion
  728.         ;; (undone by mh-prompt-for-folder)
  729.         (setq mh-folder-list-temp
  730.           (cons (list new-folder)
  731.             (cons
  732.              (list (concat (car (car mh-folder-list-temp)) "/"))
  733.              (cdr mh-folder-list-temp))))
  734.       (setq mh-folder-list-temp
  735.         (cons (list new-folder)
  736.               mh-folder-list-temp)))
  737.     (setq position (1+ line-end)))))
  738.     (setq mh-folder-list-partial-line (substring output position))))
  739.  
  740.  
  741. (defun mh-folder-name-p (name)
  742.   ;; Return non-NIL if NAME is possibly the name of a folder.
  743.   ;; A name (a string or symbol) can be a folder name if it begins with "+".
  744.   (if (symbolp name)
  745.       (eql (aref (symbol-name name) 0) ?+)
  746.     (and (> (length name) 0)
  747.      (eql (aref name 0) ?+))))
  748.  
  749.  
  750. ;;; Issue commands to MH.
  751.  
  752.  
  753. (defun mh-exec-cmd (command &rest args)
  754.   ;; Execute mh-command COMMAND with ARGS.
  755.   ;; The side effects are what is desired.
  756.   ;; Any output is assumed to be an error and is shown to the user.
  757.   ;; The output is not read or parsed by mh-e.
  758.   (save-excursion
  759.     (set-buffer (get-buffer-create mh-temp-buffer))
  760.     (erase-buffer)
  761.     (apply 'call-process
  762.        (expand-file-name command mh-progs) nil t nil
  763.        (mh-list-to-string args))
  764.     (if (> (buffer-size) 0)
  765.     (save-window-excursion
  766.       (switch-to-buffer-other-window mh-temp-buffer)
  767.       (sit-for 5)))))
  768.  
  769.  
  770. (defun mh-exec-cmd-error (env command &rest args)
  771.   ;; In environment ENV, execute mh-command COMMAND with args ARGS.
  772.   ;; ENV is nil or a string of space-separated "var=value" elements.
  773.   ;; Signals an error if process does not complete successfully.
  774.   (save-excursion
  775.     (set-buffer (get-buffer-create mh-temp-buffer))
  776.     (erase-buffer)
  777.     (let ((status
  778.        (if env
  779.            ;; the shell hacks necessary here shows just how broken Unix is
  780.            (apply 'call-process "/bin/sh" nil t nil "-c"
  781.               (format "%s %s ${1+\"$@\"}"
  782.                   env 
  783.                   (expand-file-name command mh-progs))
  784.               command
  785.               (mh-list-to-string args))
  786.            (apply 'call-process
  787.               (expand-file-name command mh-progs) nil t nil
  788.               (mh-list-to-string args)))))
  789.       (mh-handle-process-error command status))))
  790.  
  791.  
  792. (defun mh-exec-cmd-daemon (command &rest args)
  793.   ;; Execute MH command COMMAND with ARGS in the background.
  794.   ;; Any output from command is displayed in an asynchronous pop-up window.
  795.   (save-excursion
  796.     (set-buffer (get-buffer-create mh-temp-buffer))
  797.     (erase-buffer))
  798.   (let* ((process-connection-type nil)
  799.      (process (apply 'start-process
  800.              command nil
  801.              (expand-file-name command mh-progs)
  802.              (mh-list-to-string args))))
  803.     (set-process-filter process 'mh-process-daemon)))
  804.  
  805. (defun mh-process-daemon (process output)
  806.   ;; Process daemon that puts output into a temporary buffer.
  807.   (set-buffer (get-buffer-create mh-temp-buffer))
  808.   (insert-before-markers output)
  809.   (display-buffer mh-temp-buffer))
  810.  
  811.  
  812. (defun mh-exec-cmd-quiet (raise-error command &rest args)
  813.   ;; Args are RAISE-ERROR, COMMANDS, ARGS....
  814.   ;; Execute MH command COMMAND with ARGS.  ARGS is a list of strings.
  815.   ;; Return at start of mh-temp buffer, where output can be parsed and used.
  816.   ;; Returns value of call-process, which is 0 for success,
  817.   ;; unless RAISE-ERROR is non-nil, in which case an error is signaled
  818.   ;; if call-process returns non-0.
  819.   (set-buffer (get-buffer-create mh-temp-buffer))
  820.   (erase-buffer)
  821.   (let ((value
  822.      (apply 'call-process
  823.         (expand-file-name command mh-progs) nil t nil
  824.         args)))
  825.     (goto-char (point-min))
  826.     (if raise-error
  827.     (mh-handle-process-error command value)
  828.       value)))
  829.  
  830.  
  831. (defun mh-exec-cmd-output (command display &rest args)
  832.   ;; Execute MH command COMMAND with DISPLAY flag and ARGS.
  833.   ;; Put the output into buffer after point.  Set mark after inserted text.
  834.   ;; Output is expected to be shown to user, not parsed by mh-e.
  835.   (push-mark (point) t)
  836.   (apply 'call-process
  837.      (expand-file-name command mh-progs) nil t display
  838.      (mh-list-to-string args))
  839.   (exchange-point-and-mark))
  840.  
  841.  
  842. (defun mh-exec-lib-cmd-output (command &rest args)
  843.   ;; Execute MH library command COMMAND with ARGS.
  844.   ;; Put the output into buffer after point.  Set mark after inserted text.
  845.   (apply 'mh-exec-cmd-output (expand-file-name command mh-lib) nil args))
  846.  
  847.  
  848. (defun mh-handle-process-error (command status)
  849.   ;; Raise error if COMMAND returned non-0 STATUS, otherwise return STATUS.
  850.   ;; STATUS is return value from call-process.
  851.   ;; Program output is in current buffer.
  852.   ;; If output is too long to include in error message, display the buffer.
  853.   (cond ((eql status 0)            ;success
  854.      status)
  855.     ((stringp status)        ;kill string
  856.      (error (format "%s: %s" command status)))
  857.     (t                ;exit code
  858.      (cond
  859.       ((= (buffer-size) 0)        ;program produced no error message
  860.        (error (format "%s: exit code %d" command status)))
  861.       (t
  862.        ;; will error message fit on one line?
  863.        (goto-line 2)
  864.        (if (and (< (buffer-size) (screen-width))
  865.             (eobp))
  866.            (error (buffer-substring 1 (progn (goto-char 1)
  867.                          (end-of-line)
  868.                          (point))))
  869.          (display-buffer (current-buffer))
  870.          (error (format
  871.              "%s failed with status %d.  See error message in other window."
  872.              command status))))))))
  873.  
  874.  
  875. (defun mh-expand-file-name (filename &optional default)
  876.   ;; Just like `expand-file-name', but also handles MH folder names.
  877.   ;; Assumes that any filename that starts with '+' is a folder name.
  878.    (if (mh-folder-name-p filename)
  879.        (expand-file-name (substring filename 1) mh-user-path)
  880.      (expand-file-name filename default)))
  881.  
  882.  
  883. (defun mh-list-to-string (l)
  884.   ;; Flattens the list L and makes every element of the new list into a string.
  885.   (nreverse (mh-list-to-string-1 l)))
  886.  
  887. (defun mh-list-to-string-1 (l)
  888.   (let ((new-list nil))
  889.     (while l
  890.       (cond ((null (car l)))
  891.         ((symbolp (car l))
  892.          (setq new-list (cons (symbol-name (car l)) new-list)))
  893.         ((numberp (car l))
  894.          (setq new-list (cons (int-to-string (car l)) new-list)))
  895.         ((equal (car l) ""))
  896.         ((stringp (car l)) (setq new-list (cons (car l) new-list)))
  897.         ((listp (car l))
  898.          (setq new-list (nconc (mh-list-to-string-1 (car l))
  899.                    new-list)))
  900.         (t (error "Bad element in mh-list-to-string: %s" (car l))))
  901.       (setq l (cdr l)))
  902.     new-list))
  903.  
  904. (provide 'mh-utils)
  905.  
  906. (and (not noninteractive)
  907.      mh-auto-folder-collect
  908.      (mh-make-folder-list-background))
  909.  
  910. ;;; mh-utils.el ends here
  911.